package day13.CollectionTask;

import java.util.*;

public class SendMessage {
    public static void main(String[] args) {
        // Step 1: 将以下手机号码段添加到Map集合中
        Map<String, String> phoneNumberMap = new HashMap<>();
        phoneNumberMap.put("130", "中国移动");
        phoneNumberMap.put("131", "中国联通");
        phoneNumberMap.put("132", "中国电信");
        phoneNumberMap.put("133", "中国移动");
        phoneNumberMap.put("134", "中国联通");
        phoneNumberMap.put("135", "中国电信");
        phoneNumberMap.put("136", "中国移动");
        phoneNumberMap.put("137", "中国联通");
        phoneNumberMap.put("138", "中国电信");
        phoneNumberMap.put("152", "中国移动");
        // Step 2: 从控制台输入要发送信息的号码和短信内容
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要发送信息的号码（多个号码请用英文\",\"号隔开）：");
        String phoneNumbersInput = scanner.nextLine();
        System.out.print("请输入短信内容：");
        String messageContent = scanner.nextLine();
        // Step 3: 编写短信发送类
        String[] phoneNumbers = phoneNumbersInput.split(",");
        List<String> successfulNumbers = new ArrayList<>();
        for (String phoneNumber : phoneNumbers) {
            phoneNumber = phoneNumber.trim();
            // 判断号码是否为空
            if (phoneNumber.isEmpty()) {
                System.out.println("号码不能为空");
                continue;
            }
            // 判断是否为红名单用户
            if (phoneNumberMap.containsKey(phoneNumber)) {
                System.out.println("红名单用户：" + phoneNumberMap.get(phoneNumber));
                long startTime = System.nanoTime();
//                long startTime = System.currentTimeMillis();
                // 模拟发送短信，成功概率为98%
                if (Math.random() < 0.98) {
                    successfulNumbers.add(phoneNumber);
                }
                long endTime = System.nanoTime();
//                long endTime=System.currentTimeMillis();
                double elapsedTime = endTime - startTime;
                System.out.println("红名单校验耗费时间：" + elapsedTime + "ms");
            } else {
                // 模拟发送短信，成功概率为98%
                if (Math.random() < 0.98) {
                    successfulNumbers.add(phoneNumber);
                }
            }
        }
        // Step 4: 展示发送成功的短信号码和内容
        System.out.println("\n发送成功的短信：");
        for (String number : successfulNumbers) {
            System.out.println("号码：" + number + "，内容：" + messageContent);
        }
    }
}
